<?php
include_once 'top/TopClient.php';
include_once 'top/RequestCheckUtil.php';
include_once 'top/request/UmpPromotionGetRequest.php';
include_once 'top/request/TaobaokeItemsDetailGetRequest.php';
include_once 'top/request/ItemGetRequest.php';
include_once 'TopSdk.php';

class Txy_Util_TaobaoItem {
	private $c;
	private $db_taobaoitem;
	function __construct(){
		$this->c = new TopClient;
		$this->c->appkey = '12532098';
		$this->c->secretKey = 'c22aee3f68fe4acc9015a1e9dba1cd16';
		$this->db_taobaoitem=new Application_Model_DbTable_TaobaoItems();
	}
	/**
	 * 更新商品信息，传入参数为numiid字符串，以英文逗号分割
	 * Enter description here ...
	 * @param unknown_type $numiids
	 */
	public function updateTaobaoItems($numiids){
		$numarry=explode(",", $numiids);
		$req=new TaobaokeItemsDetailGetRequest();
		$req->setFields("num_iid,click_url,shop_click_url,title,nick,desc,price,pic_url,freight_payer,item_img");
		$req->setNick("淘岫玉");
		$req->setNumIids($numiids);
		$resp=$this->c->execute($req);
		if(isset($resp->taobaoke_item_details)){
			foreach ($resp->taobaoke_item_details->taobaoke_item_detail as $item){
				$data=$this->taobaokeItemDetailToArray($item);
				$data['is_taobaoke']=1;
				//去掉$numarry数组中已经查到结果的数据
				if(in_array($data['num_iid'], $numarry)){
					for($int=0;$int<count($numarry);$int++){
						if(isset($numarry[$int])){
							if($numarry[$int]==$data['num_iid']){
								unset($numarry[$int]);
							}
						}
					}
				}
				//更新ump信息(暂时先放这里，以后测试是否非淘宝客商品都没有活动在更改)
				$req_ump = new UmpPromotionGetRequest;
				$req_ump->setItemId($data['num_iid']);
				for($int=0;$int<50;$int++){
					$resp_ump = $this->c->execute($req_ump);
					if(isset($resp_ump->code)&&$resp_ump->code==7){
						continue;
					}elseif(isset($resp_ump->promotions->promotion_in_item->promotion_in_item)){
						$umpInfo=$resp_ump->promotions->promotion_in_item->promotion_in_item;
						$data['is_promotion']=1;
						$data['promotion_name']=(string)$umpInfo->name;
						$data['promotion_price']=(string)$umpInfo->item_promo_price;
						$data['promotion_end_time']=(string)$umpInfo->end_time;
						break;
					}else{
						$data['is_promotion']=0;
						$data['promotion_name']="";
						$data['promotion_price']="";
						$data['promotion_end_time']="";
						break;
					}
					if($int==49){
						$data['is_promotion']=2;//表示尚无法确定
					}
				}
				$data['is_onsale']=1;
				$data['update_time']=new Zend_Db_Expr('now()');
				$numiid=$data['num_iid'];
				unset($data['num_iid']);
				$this->db_taobaoitem->updateTaobaoItems($data, $numiid);
			}
			//查询并更新非淘宝客商品
			foreach ($numarry as $numiid){
				$this->updateItemDetail($numiid);
			}			
		}else{
			foreach ($numarry as $numiid){
				$this->updateItemDetail($numiid);
			}
		}
	}
	/**
	 * 更新促销信息
	 * Enter description here ...
	 * @param unknown_type $numiid
	 */
	public function updateUmp($numiid){
		$req_ump = new UmpPromotionGetRequest;
		$req_ump->setItemId($numiid);
		for($int=0;$int<100;$int++){
			$resp_ump = $this->c->execute($req_ump);
			if(isset($resp_ump->code)&&$resp_ump->code==7){
				continue;
			}elseif(isset($resp_ump->promotions->promotion_in_item->promotion_in_item)){
				$umpInfo=$resp_ump->promotions->promotion_in_item->promotion_in_item;
				$data['is_promotion']=1;
				$data['promotion_name']=(string)$umpInfo->name;
				$data['promotion_price']=(string)$umpInfo->item_promo_price;
				$data['promotion_end_time']=(string)$umpInfo->end_time;
				echo $numiid."|ump success|".$int."<br/>";
				break;
			}else{
				$data['is_promotion']=0;
				$data['promotion_name']="";
				$data['promotion_price']="";
				$data['promotion_end_time']="";
				echo $numiid."|no ump success|".$int."<br/>";
				break;
			}
			if($int==99){
				$data['is_promotion']=2;//表示尚无法确定
				echo $numiid."|out bound|".$int."<br/>";
			}
		}
		$data['update_time']= new Zend_Db_Expr('now()');
		$this->db_taobaoitem->updateTaobaoItems($data, $numiid);
	}
	
	/**
	 * 
	 * 将淘宝客api返回的对象转为数组
	 * @param unknown_type $item
	 */
	private function taobaokeItemDetailToArray($item){
		$data=array();
		$data['num_iid']=(string)$item->item->num_iid;
		$data['click_url']=(string)$item->click_url;
		$data['shop_click_url']=(string)$item->shop_click_url;
		$data['title']=(string)$item->item->title;
		$data['desc']=(string)$item->item->desc;
		$data['price']=(string)$item->item->price;
		$data['pic_url']=(string)$item->item->pic_url;
		$data['freight_payer']=(string)$item->item->freight_payer;
		$data['item_imgs']=$item->item->item_imgs->asXML();
		return $data;
	}
	/**
	 * 更新非淘宝客商品信息,如果没有找到则is_onsale自加1
	 * Enter description here ...
	 * @param unknown_type $numiid
	 */
	private function updateItemDetail($numiid){
		$req=new ItemGetRequest();
		$req->setFields("num_iid,detail_url,title,desc,nick,price,pic_url,freight_payer,item_img");
		$req->setNumIid($numiid);
		$resp = $this->c->execute($req);
		if(isset($resp->item)){
			$itemInfo=$resp->item;
			$data['is_taobaoke']=0;
			$data['is_promotion']=0;
			$data['promotion_name']="";
			$data['promotion_price']="";
			$data['promotion_end_time']="";
			$data['is_onsale']=1;
			$data['title']=(string)$itemInfo->title;
			$data['desc']=(string)$itemInfo->desc;
			$data['price']=(string)$itemInfo->price;
			$data['pic_url']=(string)$itemInfo->pic_url;
			$data['freight_payer']=(string)$itemInfo->freight_payer;
			$data['item_imgs']=$itemInfo->item_imgs->asXML();
			$data['update_time']=new Zend_Db_Expr('now()');
			$this->db_taobaoitem->updateTaobaoItems($data, $numiid);
		}else{
			$res=$this->db_taobaoitem->getItemsByNumiid($numiid);
			$data=array("is_onsale"=>$res['is_onsale']+1,"update_time"=>new Zend_Db_Expr('now()'));
			$this->db_taobaoitem->updateTaobaoItems($data, $numiid);
		}
	}
}

?>